import mysql from 'mysql2/promise'
import config from '../config'

export let connection: mysql.Connection | null = null

const createConnetion = async () => {
    connection = await mysql.createConnection({
        user: config.db.username,
        password: config.db.password,
        database: config.db.database,
        host: config.db.host,
        // rowsAsArray: true
    })
    console.log('Database connected')
    console.log(await connection.query('show tables;'))
}

export const query = async (sql: string, values?: any[]) => {
    const [rows] = await connection!.query(sql, values)
    return rows
}

createConnetion()
